package com.zhiwang.planet.ui.my

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import com.zhiwang.planet.R
import com.zhiwang.planet.ui.BaseActivity
import com.zhiwang.planet.ui.FragmentChangeManager
import com.zhiwang.planet.ui.WebViewFragment
import com.zhiwang.planet.util.Store
import com.zhiwang.planet.util.suicideAfterTransition
import com.zhiwang.planet.widget.StyleTab
import kotlinx.android.synthetic.main.activity_details.*
import java.util.*

class GameCenterActivity : BaseActivity() {

    private var currentTab: Int = 0
    private var fragmentChangeManager: FragmentChangeManager? = null
    private lateinit var fragments: ArrayList<Fragment>

    private val onTabSelectListener = View.OnClickListener { view ->
        val index = styleTabLayout.indexOfChild(view)
        select(index)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_game_center)
        init()
    }

    private fun init() {

        fragments = arrayListOf(
                WebViewFragment.newInstance(Store.URL_GAME_, false),
                WebViewFragment.newInstance(Store.URL_TASK_, false))

        fragmentChangeManager = FragmentChangeManager(supportFragmentManager, R.id.container, fragments)

        val count = styleTabLayout.childCount

        for (i in 0 until count) {
            val styleTab = styleTabLayout.getChildAt(i) as StyleTab

            styleTab.setOnClickListener(onTabSelectListener)
        }


    }

    fun select(index: Int) {

        val count = styleTabLayout.childCount
        currentTab = index

        for (i in 0 until count) {
            val styleTab = styleTabLayout.getChildAt(i) as StyleTab
            styleTab.isChecked = i == index
        }

        fragmentChangeManager?.setFragments(index)

    }

    fun suicide(view: View) {
        suicideAfterTransition()
    }


}